package com.hnucm.cooklife.presenter.home;

import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.bean.db.Foll;
import com.hnucm.cooklife.contract.foll.Contract;
import com.hnucm.cooklife.model.FollModel;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class FollPresenter extends BasePresenter<Contract.IFollView> implements Contract.IFollPresenter {

    Contract.IFollModel mIFollModel;

    public FollPresenter() {
        mIFollModel = new FollModel();
    }

    @Override
    public void loadFoll(int owner) {
        mIFollModel.loadFoll(owner)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Foll>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull List<Foll> follList) {
                        if(isViewAttached()){

                            getView().onLoadFoll(follList);

                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        if (isViewAttached()) {
                            getView().onLoadingFailed();
                            System.out.println(e.getMessage());

                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void refreshFoll(int owner) {
        mIFollModel.refreshFoll(owner)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Foll>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull List<Foll> follList) {
                        if(isViewAttached()){
                            getView().onRefreshFoll(follList);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        if (isViewAttached()) {
                            getView().onLoadingFailed();
                            System.out.println(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
